$(function() {
    $("button[href]").click(function(){
        $.get("/selectMode", {
           mode : $(this).attr("mode")
       }, function(result) {
           if (result === true) {
               window.location.href=window.location.href;
               
           }
       });
   });
    $("#end").val($("#timepicker_end").val());
	 $("#start").val($("#timepicker_start").val());
    $("[title]").tooltip();
    $(".selectpicker").selectpicker();
    $('#radioset').buttonset();
    $("#query_time").click(function(){
        $.get("/book/time",{date:$("#datepicker").val(),endTime:$("#timepicker_end").val(),  beginTime:$("#timepicker_start").val(), amount:$("#amount").text()},
                function(data) {
                var html = "";
                
                for ( var i = 0; i < data.length; i++) {
                    var row = '<tr>';
                    var ev = data[i];
                    row += "<td>" + ev.id + "</td>";
                    row += "<td>" + ev.meetingName + "</td>";
                    row += "<td>" + ev.floor + "</td>";
                    row += '<td data-value="' + ev.hasLync + '">' + judge(ev.hasLync) + "</td>";
                    row += '<td data-value="' + ev.hasIt + '">' + judge(ev.hasIt) + "</td>";
                    row += '<td data-value="' + ev.hasPhone + '">' + judge(ev.hasPhone) + "</td>";
                    row += '<td data-value="' + ev.hasTv + '">' + judge(ev.hasTv) + "</td>";
                    row += '<td data-value="' + ev.maxNums + '">' + ev.maxNums + "</td>";
                    row += "<td>" + ev.remark + "</td>";
                    row += "<td><button class='ui-button-primary book' onclick='revert("+ev.id+",0,0,"+$("#datepicker").val()+" "+$("#timepicker_start").val()+','+$("#datepicker").val()+" "+$("#timepicker_end").val()+"')'"+ " meetingid='"+ev.id+"'>预订</button></td>";
                    row += '</tr>';
                    html += row;
                }
               
                $('table tbody').html(html).trigger('footable_initialize');
                $("tfoot tr td").show();
                $("#filter").val("");
            });
    });
    $('#datepicker').datepicker({
        changeMonth : true,
        changeYear : true,
        dateFormat : 'yy-mm-dd',
        minDate : new Date(),
        maxDate : '+1m',
        showButtonPanel : true,
        currentText : 'Today'
    }).tooltip();
    $('#timepicker_start').timepicker({
        minuteStep : 30,
        defaultTime : '09:00 AM'
    }).tooltip();
    $('#timepicker_end').timepicker({
        minuteStep : 30,
        defaultTime : '11:00 AM'
    }).tooltip();
    $('#h-slider').slider({
        range : "min",
        min : 0,
        max : 8,
        value : 4,
        slide : function(event, ui) {
            $("#amount").text(ui.value / 2);
        }
    }).tooltip();
    $('#amount').tooltip();
    $('#datepicker').datepicker('setDate', new Date());
    if (document.getElementById("tabs")) {
        $("#tabs").tabs();
        $.get("/meetings/all", function(result) {
            var $a = $(result[0]);
            var $b = $(result[1]);
            var floor = 1;
            var html = "";
            $a.each(function() {
                if ($(this)[0].floor != floor) {
                    floor = $(this)[0].floor;
                    html += "<br/>"
                }
                html = html + "<div class='span3' >" + $(this)[0].building + $(this)[0].floor + $(this)[0].number + "</div>";
            });
            $("#tabs-a").html(html);
            floor = 1
            html = "";
            $b.each(function() {
                if ($(this)[0].floor != floor) {
                    floor = $(this)[0].floor;
                    html += "<br/>"
                }
                html = html + "<div class='span3' >" + $(this)[0].building + $(this)[0].floor + $(this)[0].number + "</div>";
            });
            $("#tabs-b").html(html);
        });
    }
    $('.footable').footable();
    $('.clear-filter').click(function(e) {
        e.preventDefault();
        $('table').trigger('footable_clear_filter');
        $("select[class^='filter-']").each(function() {
            // $(this).val('全部');
        });
        $("#filter").val("");
    });
    
    $("select[class^='filter-']").change(function(e) {
        var href = '/meetings/filter?sql=';
        $("select[class^='filter-']").each(function() {
            var sql = $(this).find("option:selected").attr("sql");
            href += sql;
        });
        $.ajax({
            url : href,
            success : function(data) {
                var html = "";
                
                for ( var i = 0; i < data.length; i++) {
                    var row = '<tr>';
                    var ev = data[i];
                    row += "<td>" + ev.id + "</td>";
                    row += "<td>" + ev.meetingName + "</td>";
                    row += "<td>" + ev.floor + "</td>";
                    row += '<td data-value="' + ev.hasLync + '">' + judge(ev.hasLync) + "</td>";
                    row += '<td data-value="' + ev.hasIt + '">' + judge(ev.hasIt) + "</td>";
                    row += '<td data-value="' + ev.hasPhone + '">' + judge(ev.hasPhone) + "</td>";
                    row += '<td data-value="' + ev.hasTv + '">' + judge(ev.hasTv) + "</td>";
                    row += '<td data-value="' + ev.maxNums + '">' + ev.maxNums + "</td>";
                    row += "<td>" + ev.remark + "</td>";
                    row += "<td><button class='ui-button-primary book' onclick='revert(this)' meetingid='"+ev.id+"'>预订</button></td>";
                    row += '</tr>';
                    html += row;
                }
                
                $('table tbody').html(html).trigger('footable_initialize');
                $("tfoot tr td").show();
                $("#filter").val("");
            }
        });
    });
    $("#query_meeting").click(function(){
    	var html="";
    	$.get("/book/meeting",{
    		meetingId:$("#meetingroom").val() ,amount:$("#amount").text()
    		},
    		function(data){
    		 var row = '<tr meetingid='+data.id+'>';
             row += "<td>" + data.meetingName + "</td>";
             row += "<td>" + new Date(parseFloat(data.bookStartTime)).pattern("yyyy-MM-dd HH:mm:ss") + "</td>";
             row += "<td>" +new Date(parseFloat(data.bookEndTime)).pattern("yyyy-MM-dd HH:mm:ss") + "</td>";
             row += "<td><button class='ui-button-primary book' onclick='revert("+data.id+","+data.bookStartTime+","+data.bookEndTime+")' meetingid='"+data.id+"'>预订</button></td>";
             row += '</tr>';
             html += row;
             $('table tbody').html(html).trigger('footable_initialize');
    	});
    	
    });
    
});
function judge(data) {
    if (data == 0)
        return '<span class="status-metro status-disabled" title="Disabled">否</span>';
    else
        return '<span class="status-metro status-active" title="Active">是</span>';
}
function revert(meetingId,bookStartTime,bookEndTime,startTime,endTime){
	window.location.href="/status?meetingId="+meetingId+"&bookStartTime="+bookStartTime+"&bookEndTime="+bookEndTime+"&startTime="+startTime+"&endTime="+endTime;
	$(".cur",window.parent.document).each(function(){
			$(this).removeClass("cur");
	});
	$("#status",window.parent.document).addClass("cur");
// var meetingid=$(element).attr("meetingid");
// var selectdate=$("#datepicker").val();
// $("#meeting").val(meetingid);
// $("#reservebox").dialog({
// autoOpen : false,
// height : 450,
// width : 400,
// title : '预约会议室于 ' + selectdate,
// modal : true,
// position : "center",
// draggable : false,
// beforeClose : function(event, ui) {
// $.validationEngine.closePrompt("#meeting");
// $.validationEngine.closePrompt("#start");
// $.validationEngine.closePrompt("#end");
// },
// buttons : {
// "关闭" : function() {
// $(this).dialog("close");
// },
// "预约" : function() {
// if ($("#reserveformID").validationEngine({
// returnIsValid : true
// })) {
// var startdatestr = $("#start").val();
// var enddatestr = $("#end").val();
// var title = $("#title").val(); // 标题
// var details = $("#details").val(); // 内容
// var confid = $("#meeting").val();
// var repweeks = $("#repweeks").val();
// if (repweeks == null) {
// repweeks = 0;
// }
//                    
// var startdate = parseDate(selectdate + "T" + startdatestr);
// var enddate =parseDate(selectdate + "T" + enddatestr);
// var schdata = {
// startdate : startdate.pattern("yyyy-MM-dd HH:mm:ss"),
// enddate : enddate.pattern("yyyy-MM-dd HH:mm:ss"),
// repweeks : repweeks,
// meetingId : confid,
// title : title,
// details : details
// };
//                    
// $.get("/meetings/book", schdata, function(data) {
// alert(data?"预订成功！":"预订失败！");
// });
// $(this).dialog("close");
// }
// }
// }
// });
// $("#reservebox").dialog("open");
}
